<template>
  <el-dialog :title="info.title" :visible.sync="visible" center width="660px" :before-close="handleCancel">
    <div class="">
      <el-button class="fr" type="primary" size="mini" @click="handleAdd">新 增</el-button>
      <el-tag
        class="mr10 mb5"
        v-for="tag in list"
        :key="tag.id"
        @close="handleDelete(tag)"
        closable>
        {{tag.nickname}}
      </el-tag>
    </div>
    <Dialog ref="dialog" :get-data="getList" :info="dialog" />
  </el-dialog>
</template>

<script>
import { punchStaffs, delPunchStaff } from '@/api/punch'
import Dialog from './staff-dialog'

export default {
  components: { Dialog },
  inject: ['app'],
  props: {
    info: {
      type: Object,
      default: null
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  data() {
    return {
      form: {},
      dialog: {
        edit: {},
        title: '',
        type: ''
      },
      list: [],
      visible: false
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.$set(this, 'list', [])
        this.$set(this, 'form', this.info.edit)
        this.getList()
      }
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  mounted() {
  },
  methods: {
    getList(e) {
      punchStaffs({
        ...this.filter,
        ...e,
        ...{lops_id: this.form.lops_id}
      }).then(response => {
        this.list = response.list
        this.filter = response.filter
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    handleDelete(e) {
      this.$confirm('此操作将删除数据信息，数据将无法恢复, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        delPunchStaff({
          ids: e['id']
        }).then(response => {
          this.getList()
          this.$message.success('删除成功')
        })
      })
    },
    handleAdd(e) {
      this.dialog.title = '新增人员'
      this.dialog.type = 'add'
      e = {}
      e.lops_id = this.form.lops_id
      this.app.dialogParam(this.dialog.edit, ['lops_id', 'su_id'], e)
      this.$refs['dialog'].visible = true
    },
    handleCancel(done) {
      if (this.$refs['formDate']) this.$refs['formDate'].resetFields()
      if (done && typeof done === Function) {
        done()
      } else {
        this.visible = false
      }
    }
  }
}
</script>

<style scoped>
</style>
